﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Models;
using MySql.Data.MySqlClient;
using Common;
using System.Data;

namespace DAL
{
    public class DealerDAL
    {
        //保存
        public int SaveData(DealerInfo d) {

            return SqlHelper.ExecuteNonQuery("insert into dealerinfo (dname,dphone,daddress,isdel,adduser,edituser,addtime,edittime,dcontact) values(@dname,@dphone,@daddress,@isdel,@adduser,@edituser,@addtime,@edittime,@dcontact)",
                new MySqlParameter("@dname", d.Dname), new MySqlParameter("@dphone", d.Dphone),new MySqlParameter("@daddress",d.Daddress),new MySqlParameter("@isdel","0"),
                new MySqlParameter("@adduser",RemeberName.Username),new MySqlParameter("@edituser",RemeberName.Username),new MySqlParameter("@addtime",DateTime.Now.ToString()),new MySqlParameter("@edittime",DateTime.Now.ToString()),new MySqlParameter("@dcontact",d.Dcontact));
        }
        //更新
        public int UpdateData(DealerInfo d) {
            return SqlHelper.ExecuteNonQuery("update dealerinfo set dname=@dname,dphone=@dphone,daddress=@daddress,edituser=@edituser,edittime=@edittime,dcontact=@dcontact where id=@id",
                new MySqlParameter("@dname", d.Dname), new MySqlParameter("@dphone", d.Dphone), new MySqlParameter("@daddress", d.Daddress), new MySqlParameter("@edituser", RemeberName.Username),
                new MySqlParameter("@edittime",DateTime.Now.ToString()),new MySqlParameter("@dcontact",d.Dcontact),new MySqlParameter("@id",d.Id));
        }
        //删除
        public int DelById(string id) {
            return SqlHelper.ExecuteNonQuery("update dealerinfo set isdel=1 ,edituser = @edituser,edittime=@edittime where id= @id",
                new MySqlParameter("@edituser",RemeberName.Username),new MySqlParameter("@edittime",DateTime.Now.ToString()),new MySqlParameter("@id",id));
        }
        //查询所有
        public DataTable DealerListAll(string name, string phone) {
            string sql = string.Empty;
            if (!string.IsNullOrWhiteSpace(name))
            {
                sql += " and dname like '%" + name + "%' ";
            }
            if (!string.IsNullOrWhiteSpace(phone))
            {
                sql += " and dphone like '%" + phone + "%' ";
            }
            return SqlHelper.ExecuteDataTable("select * from dealerinfo where isdel=0" + sql + " order by edittime desc ");
        }
        //根据id查询
        public DealerInfo GetById(string id) {
            DataTable dt = SqlHelper.ExecuteDataTable("select * from dealerInfo where id =@id", new MySqlParameter("@id", id));
            if (dt.Rows.Count == 1)
            {
                return ToDealer(dt.Rows[0]);
            }
            else {
                throw new Exception("存在重复值,请联系管理员");
            }
        }
        //转换成模型
        public DealerInfo ToDealer(DataRow dr) {
            DealerInfo d = new DealerInfo();
            d.Id = Convert.ToInt32(dr["id"]);
            d.Dphone = dr["dphone"].ToString();
            d.Dname = dr["dname"].ToString();
            d.Daddress = dr["daddress"].ToString();
            d.AddUser = dr["adduser"].ToString();
            d.Dcontact = dr["dcontact"].ToString();
            return d;
        }
    }
}
